package cn.wonderland.pet.service.impl;

import cn.wonderland.pet.domain.PetType;
import cn.wonderland.pet.mapper.PetTypeMapper;
import cn.wonderland.pet.service.IPetTypeService;
import cn.wonderland.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wonderland
 * @since 2023-05-13
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements IPetTypeService {

    @Autowired
    private PetTypeMapper petTypeMapper;

    /**
     * 无线级数--宠物类型
     */
    @Override
    public List<PetType> tree() {
        List<PetType> result = new ArrayList<>();
        // 查询所有
        List<PetType> typeList = findAll();
        Map<Long, PetType> map = typeList.stream().collect(Collectors.toMap(PetType::getId, e -> e));
        typeList.forEach(type ->{
            if (type.getPid() == null) {
                result.add(type);
            } else {
                PetType petType = map.get(type.getPid());
                petType.getChildren().add(type);
            }
        });
        return result;
    }
}
